/**
 * 插件扩展管理
 * 氚云API管理系统 - 插件扩展功能
 * @copyright Alyr.net
 * @author Alyr.net
 */

// 显示上传插件弹窗
function showUploadPluginModal() {
    document.getElementById('uploadPluginModal').classList.add('show');
    
    // 添加插件标识输入限制
    const pluginIdInput = document.getElementById('plugin-id');
    if (pluginIdInput) {
        pluginIdInput.addEventListener('input', function(e) {
            // 只允许英文字母和下划线
            this.value = this.value.replace(/[^a-zA-Z_]/g, '');
        });
        
        pluginIdInput.addEventListener('blur', function(e) {
            // 转换为小写
            this.value = this.value.toLowerCase();
        });
    }
}

// 隐藏上传插件弹窗
function hideUploadPluginModal() {
    document.getElementById('uploadPluginModal').classList.remove('show');
    document.getElementById('uploadPluginForm').reset();
}

// 上传插件
function uploadPlugin() {
    const form = document.getElementById('uploadPluginForm');
    if (!form.checkValidity()) {
        alert('请填写所有必填项');
        form.reportValidity();
        return;
    }
    
    // 验证插件标识
    const pluginId = document.getElementById('plugin-id').value.trim();
    if (!pluginId) {
        alert('请输入插件标识');
        return;
    }
    
    if (!/^[a-zA-Z_]+$/.test(pluginId)) {
        alert('插件标识只能包含英文字母和下划线');
        return;
    }

    const formData = new FormData(form);
    
    // 这里添加上传逻辑
    fetch('plugin/upload-plugin.php', {
        method: 'POST',
        body: formData
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            alert('插件上传成功！');
            hideUploadPluginModal();
            // 刷新插件列表
            location.reload();
        } else {
            alert('上传失败：' + (data.message || '未知错误'));
        }
    })
    .catch(error => {
        alert('上传失败：' + error.message);
    });
}

// 配置插件
function configurePlugin(pluginId) {
    showPluginConfigModal(pluginId);
}

// 显示插件配置弹窗
function showPluginConfigModal(pluginId) {
    if (pluginId === 'sms') {
        const modal = document.getElementById('smsConfigModal');
        const container = document.getElementById('sms-config-container');
        
        // 显示弹窗
        modal.classList.add('show');
        
        // 加载短信配置内容
        container.innerHTML = '<div style="text-align:center;padding:40px;color:#999;">正在加载配置...</div>';
        
        fetch('plugin/sms/sms-config.php')
            .then(response => response.text())
            .then(html => {
                container.innerHTML = html;
            })
            .catch(error => {
                container.innerHTML = '<div style="text-align:center;padding:40px;color:#e74c3c;">加载失败：' + error.message + '</div>';
            });
    } else if (pluginId === 'dingtalk') {
        const modal = document.getElementById('dingtalkConfigModal');
        const container = document.getElementById('dingtalk-config-container');
        
        // 显示弹窗
        modal.classList.add('show');
        
        // 加载钉钉配置内容
        container.innerHTML = '<div style="text-align:center;padding:40px;color:#999;">正在加载配置...</div>';
        
        fetch('plugin/dingtalk/dingtalk-config.php')
            .then(response => response.text())
            .then(html => {
                container.innerHTML = html;
            })
            .catch(error => {
                container.innerHTML = '<div style="text-align:center;padding:40px;color:#e74c3c;">加载失败：' + error.message + '</div>';
            });
    } else if (pluginId === 'wechat') {
        const modal = document.getElementById('wechatConfigModal');
        const container = document.getElementById('wechat-config-container');
        
        // 显示弹窗
        modal.classList.add('show');
        
        // 加载微信配置内容
        container.innerHTML = '<div style="text-align:center;padding:40px;color:#999;">正在加载配置...</div>';
        
        fetch('plugin/wechat/wechat-config.php')
            .then(response => response.text())
            .then(html => {
                container.innerHTML = html;
            })
            .catch(error => {
                container.innerHTML = '<div style="text-align:center;padding:40px;color:#e74c3c;">加载失败：' + error.message + '</div>';
            });
    } else if (pluginId === 'feishu') {
        const modal = document.getElementById('feishuConfigModal');
        const container = document.getElementById('feishu-config-container');
        
        // 显示弹窗
        modal.classList.add('show');
        
        // 加载飞书配置内容
        container.innerHTML = '<div style="text-align:center;padding:40px;color:#999;">正在加载配置...</div>';
        
        fetch('plugin/feishu/feishu-config.php')
            .then(response => response.text())
            .then(html => {
                container.innerHTML = html;
            })
            .catch(error => {
                container.innerHTML = '<div style="text-align:center;padding:40px;color:#e74c3c;">加载失败：' + error.message + '</div>';
            });
    }
}

// 隐藏插件配置弹窗
function hidePluginConfigModal(pluginId) {
    if (pluginId === 'sms') {
        const modal = document.getElementById('smsConfigModal');
        modal.classList.remove('show');
    } else if (pluginId === 'dingtalk') {
        const modal = document.getElementById('dingtalkConfigModal');
        modal.classList.remove('show');
    } else if (pluginId === 'wechat') {
        const modal = document.getElementById('wechatConfigModal');
        modal.classList.remove('show');
    } else if (pluginId === 'feishu') {
        const modal = document.getElementById('feishuConfigModal');
        modal.classList.remove('show');
    }
}

// 切换插件状态
function togglePluginStatus(pluginId) {
    alert('切换插件状态：' + pluginId);
    // 这里添加启用/禁用逻辑
}

// 暴露到全局作用域
window.showUploadPluginModal = showUploadPluginModal;
window.hideUploadPluginModal = hideUploadPluginModal;
window.uploadPlugin = uploadPlugin;
window.configurePlugin = configurePlugin;
window.showPluginConfigModal = showPluginConfigModal;
window.hidePluginConfigModal = hidePluginConfigModal;
window.togglePluginStatus = togglePluginStatus;
